home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiniExamples / AppKit / AtYourService / Verifier.m < prev    next >
Text File  |  1995-06-12  |  2KB  |  79 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Verifier.h"
  5. #import <string.h>
  6. #import <appkit/publicWraps.h>
  7. #import <appkit/Matrix.h>
  8. #import <appkit/Cell.h>
  9. #import <appkit/Text.h>
  10.  
  11. /*
  12.  *
  13.  * The Verifier Object
  14.  *
  15.  * This Object is a Text Delegate that enforces valid data entry on
  16.  * Forms that use it as a Text Delegate.  The FormCells must have their
  17.  * Entry types set ( [cellname setEntryType:NX_FLOATTYPE] for instance.
  18.  *
  19.  * The Verifier also enforces a length restriction.  In this example,  it is 
  20.  * used for the separator,  which must be a single character.
  21.  *
  22.  * Whenever the user type a character,  the FormCell's validity is checked,
  23.  * and invalid changes are rejected.
  24.  *
  25.  */
  26.  
  27. @implementation Verifier
  28.  
  29. - textDidGetKeys:sender isEmpty:(BOOL)flag
  30. {
  31.     id testCell;
  32.     testCell = [[sender delegate] selectedCell];
  33.     if (! flag) {
  34.         char testString[500];
  35.         int length, testLength;
  36.         strcpy(testString,[testCell stringValue]);
  37.         if ( ! [testCell isEntryAcceptable:[testCell stringValue]] ) {
  38.         /*
  39.          *  set the selection to the invalid character, and delete it
  40.          */
  41.             int badChar;
  42.             badChar=strlen(testString);
  43.             [sender setSel:badChar-1:badChar];
  44.             [sender delete:self];
  45.             NXBeep();
  46.             strcpy(testString,[testCell stringValue]);
  47.         }
  48.       /*
  49.        * Check the length of the entry as well.  The tag is used to set the 
  50.        * valid length. If Tag is zero,  skip this check.
  51.        */
  52.         length=[testCell tag];
  53.         if ( length) {
  54.             testLength = strlen(testString);
  55.             if ( length == 1 ) {
  56.               /*
  57.                * Special case for length == 1: cut off the first part of the
  58.                * string,  leaving the last char typed,  don't beep
  59.                */
  60.                 if ( testLength > 1 ) {
  61.                     [sender setSel:0:testLength-1];
  62.                     [sender delete:self];
  63.                     }
  64.                 [sender setSel:0:1];
  65.                 }
  66.             else if ( testLength > length ) {
  67.               /*
  68.                * Length is invalid, cut off some chars
  69.                */
  70.                     [sender setSel:length-1:testLength];
  71.                     [sender delete:self];               
  72.                     NXBeep();
  73.                 }
  74.         }
  75.     }
  76.     return self;
  77. }
  78. @end
  79.